package GUI;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import GUI.manager.VentanaFactory;
import GUI.manager.VentanaManager;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;

import core.model.Carrito;
import core.thread.HiloBusquedaGuardar;
import core.thread.TablaPDF;

public class panelGuardarPdf extends javax.swing.JPanel implements ActionListener {

   VentanaManager ventanaManager=VentanaFactory.getInstance();
	
    public panelGuardarPdf() {
    	
        initComponents();
    }

                              
    private void initComponents() {

        tfRuta = new javax.swing.JTextField();
        bTitulo = new javax.swing.JButton();
        eRuta = new javax.swing.JLabel();
        bAtras = new javax.swing.JButton();
        botonSalir = new javax.swing.JButton();
        bGuardar = new javax.swing.JButton();
        bBuscar = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        tfRuta.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
        tfRuta.setForeground(new java.awt.Color(102, 102, 102));

        bTitulo.setIcon(new javax.swing.ImageIcon("src/GUI/resources/encabezadoMain.png")); // NOI18N
        bTitulo.setBorder(null);
        bTitulo.setBorderPainted(false);
        bTitulo.addActionListener(this);
        bTitulo.setBackground(new java.awt.Color(255, 255, 255));

        eRuta.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
        eRuta.setForeground(new java.awt.Color(102, 102, 102));
        eRuta.setText("RUTA");

        bAtras.setBackground(new java.awt.Color(255, 255, 255));
        bAtras.setIcon(new javax.swing.ImageIcon("src/GUI/resources/boton_atras.png")); // NOI18N
        bAtras.setBorder(null);
        bAtras.addActionListener(this);
       

        botonSalir.setBackground(new java.awt.Color(255, 255, 255));
        botonSalir.setIcon(new javax.swing.ImageIcon("src/GUI/resources/menu-salir.png")); // NOI18N
        botonSalir.setBorderPainted(false);
        botonSalir.addActionListener(this);

        bGuardar.setBackground(new java.awt.Color(255, 255, 255));
        bGuardar.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
        bGuardar.setForeground(new java.awt.Color(243, 15, 120));
        bGuardar.setIcon(new javax.swing.ImageIcon("src/GUI/resources/botonGuardar.png")); // NOI18N
        bGuardar.setBorder(null);
        bGuardar.addActionListener(this);
        

        bBuscar.setIcon(new javax.swing.ImageIcon("src/GUI/resources/lupa.jpg")); // NOI18N
        bBuscar.setBorder(null);
        bBuscar.addActionListener(this);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(bTitulo))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(bAtras)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botonSalir)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(130, 130, 130)
                                .addComponent(eRuta)
                                .addGap(18, 18, 18)
                                .addComponent(tfRuta, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(bBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(242, 242, 242)
                                .addComponent(bGuardar)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(bTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(eRuta)
                                .addComponent(tfRuta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(bBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(bGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(botonSalir))
                    .addComponent(bAtras))
                .addContainerGap(23, Short.MAX_VALUE))
        );
    }                    
    private javax.swing.JButton bAtras;
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bGuardar;
    private javax.swing.JButton bTitulo;
    private javax.swing.JButton botonSalir;
    private javax.swing.JLabel eRuta;
    private javax.swing.JTextField tfRuta;
    private JFrame actual;
   	private JPanel estePanel = this;
	
    
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == botonSalir) {
			actual = (JFrame) UtilidadesGUI
					.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelSalir(29));
			actual.setPreferredSize(new Dimension(610, 330));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bAtras) {
			actual = (JFrame) UtilidadesGUI
					.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelHistorial());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		
		if(e.getSource() == bBuscar)
		{
			HiloBusquedaGuardar guardar=new HiloBusquedaGuardar();
			guardar.arrancarHilo();
			tfRuta.setText(guardar.getRuta());
			System.out.println(tfRuta.getText());
		}
		if (e.getSource() == bTitulo) {
			actual = (JFrame) UtilidadesGUI
					.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelPrincipal());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if(e.getSource()==bGuardar)
		{
			List<Carrito> carritos=ventanaManager.findByLista(panelHistorial.codigo_lista);
			System.out.println(panelHistorial.codigo_lista);
			TablaPDF tablapdf=new TablaPDF(tfRuta.getText(),carritos);
			tablapdf.arrancarHilo();
			actual = (JFrame) UtilidadesGUI
					.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelHistorial());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);
		}
		
	}
                  
}
